package com.mall.biz.search;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.mall.MallActivityService;
import com.mall.client.mall.MallService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.client.region.AreaService;
import com.mall.client.region.CityService;
import com.mall.client.region.ProvinceService;
import com.mall.client.search.MallLuceneBuildService;
import com.mall.common.util.bit.PropertiesConfigure;
import com.mall.constant.PropertiesKey;
import com.mall.pojo.Mall;
import com.mall.pojo.MallActivity;
import com.mall.pojo.MallGroupBuy;
import com.mall.query.Page;
import com.mall.query.search.MallSearchQuery;
import com.mall.searchdo.SearchResultIdDO;
import com.mall.vo.common.IdAndTitleAndCountVO;
import com.mall.vo.mall.MallListVO;
import com.mall.web.util.MallUtil;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Rectangle;

@Service
public class MallSearchBizImpl {

	private Logger log = LoggerFactory.getLogger(MallSearchBizImpl.class);

	@Autowired
	private MallLuceneBuildService mallLuceneBuildService;
	@Autowired
	private MallService mallService;
	@Autowired
	private PropertiesConfigure propertiesConfigure;
	@Autowired
	private MallGroupbBuyService mallGroupbBuyService;
	@Autowired
	private MallActivityService mallActivityService;
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private CityService cityService;
	@Autowired
	private AreaService areaService;

	/**
	 * 店铺搜索
	 * 
	 * @param mallSearchQuery
	 * @param page
	 * @return
	 */
	public List<MallListVO> getMallVOList(MallSearchQuery mallSearchQuery, Page page) {
		List<MallListVO> mallVOList = new ArrayList<>();
		List<SearchResultIdDO> searchResultIdDOList = null;
		try {
			searchResultIdDOList = this.mallLuceneBuildService.searchEshopFromLucene(mallSearchQuery, getSearchDir(),
					page.getLength());
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (searchResultIdDOList != null && searchResultIdDOList.size() > 0) {
			searchResultIdDOList = searchResultIdDOList.subList(page.getBegin(), searchResultIdDOList.size());
		}

		mallVOList = getMallVOlistFromMallList(getMallListFromSearchResultIdDO(searchResultIdDOList));
		if (mallVOList != null && mallVOList.size() > 0) {
			return mallVOList;
		}
		log.error("lucene search null .mallSearchQuery = " + mallSearchQuery);

		if (mallSearchQuery.getNearDistance() != null && mallSearchQuery.getNearDistance() > 0) {
			Rectangle rectangle = getRectangleByLonAndlat(mallSearchQuery.getGetGaodeLongitude(),
					mallSearchQuery.getGaodeLatitude(), mallSearchQuery.getNearDistance());
			if (rectangle != null) {
				Double[] rectangles = new Double[4];
				rectangles[0] = rectangle.getMinX();
				rectangles[1] = rectangle.getMaxX();
				rectangles[2] = rectangle.getMinY();
				rectangles[3] = rectangle.getMaxY();
				mallSearchQuery.setRectangles(rectangles);
			}
		}
		List<Mall> mallDBList = this.mallService.getMallListByQuery(mallSearchQuery, page);
		mallVOList = getMallVOlistFromMallList(mallDBList);
		return mallVOList;
	}

	/**
	 * 获取正方形的四个点
	 * 
	 * @param lon
	 * @param lat
	 * @param radius
	 * @return
	 */
	public Rectangle getRectangleByLonAndlat(double lon, double lat, int radius) {
		SpatialContext geo = SpatialContext.GEO;
		Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(geo.makePoint(lon, lat),
				radius * DistanceUtils.KM_TO_DEG, geo, null);
		System.out.println(rectangle.getMinX() + "-" + rectangle.getMaxX());// 经度范围
		System.out.println(rectangle.getMinY() + "-" + rectangle.getMaxY());// 纬度范围
		return rectangle;
	}

	private String getSearchDir() {
		return propertiesConfigure.getProperties(PropertiesKey.mallsearchdir);
	}

	private List<MallListVO> getMallVOlistFromMallList(List<Mall> mallList) {
		if (mallList == null || mallList.size() <= 0) {
			return null;
		}

		List<MallListVO> mallVOList = new ArrayList<>();
		MallListVO mallVO = null;
		List<Long> mallIds = new ArrayList<>();
		IdAndTitleAndCountVO zhihuiIdAndTitleAndCountVO = null;
		for (Mall mall : mallList) {
			mallVO = new MallListVO();
			mallVO.setMall(mall);
			mallVO.setArea(this.areaService.getAreaByAreaId(mall.getAreaId()));
			mallVO.setCity(this.cityService.getCityById(mall.getCityId()));
			mallVO.setProvince(this.provinceService.getProvinceById(mall.getProvinceId()));
			mallIds.add(mall.getId());
			/**
			 * 智慧餐厅start
			 */
			zhihuiIdAndTitleAndCountVO = new IdAndTitleAndCountVO();
			boolean zhihui = MallUtil.zhihuiMall(mall);
			if (zhihui) {
				zhihuiIdAndTitleAndCountVO.setId(1l);
				zhihuiIdAndTitleAndCountVO.setTitle("智慧餐厅");
			} else {
				zhihuiIdAndTitleAndCountVO.setId(0l);
				zhihuiIdAndTitleAndCountVO.setTitle("不是智慧餐厅");
			}
			mallVO.setZhihuiIdAndTitleAndCountVO(zhihuiIdAndTitleAndCountVO);
			mallVO.setImgBegin(propertiesConfigure.getProperties(PropertiesKey.userfileserver));
			// 智慧餐厅 end
			mallVOList.add(mallVO);
		}
		/**
		 * 查询团购信息最新的1条
		 */
		List<MallGroupBuy> mallGroupBuyList = this.mallGroupbBuyService.getMallGroupBuyList4ListByMallIds(mallIds);
		if (mallGroupBuyList != null && mallGroupBuyList.size() > 0) {
			for (MallGroupBuy mallGroupBuy : mallGroupBuyList) {
				for (MallListVO mallListVO : mallVOList) {
					if (mallGroupBuy.getMallId().longValue() == mallListVO.getMall().getId().longValue()) {
						IdAndTitleAndCountVO tuanIdAndTitleAndCountVO = new IdAndTitleAndCountVO();
						tuanIdAndTitleAndCountVO.setId(mallGroupBuy.getId());
						tuanIdAndTitleAndCountVO.setTitle(mallGroupBuy.getTitle());
						mallListVO.setTuanIdAndTitleAndCountVO(tuanIdAndTitleAndCountVO);
					}
				}
			}
		}
		/**
		 * 买单的功能待定
		 */

		/**
		 * 普通活动
		 */
		List<MallActivity> mallActivitieList = this.mallActivityService.getMallActivityType1List4ListByMallIds(mallIds);
		if (mallActivitieList != null && mallActivitieList.size() > 0) {
			for (MallActivity mallActivity : mallActivitieList) {
				for (MallListVO mallListVO : mallVOList) {
					if (mallActivity.getMallId().longValue() == mallListVO.getMall().getId().longValue()) {
						IdAndTitleAndCountVO activityIdAndTitleAndCountVO = new IdAndTitleAndCountVO();
						activityIdAndTitleAndCountVO.setId(mallActivity.getId());
						activityIdAndTitleAndCountVO.setTitle(mallActivity.getActivityTitle());
						mallListVO.setActivityIdAndTitleAndCountVO(activityIdAndTitleAndCountVO);
					}
				}
			}
		}

		/**
		 * 抽奖活动
		 */
		List<MallActivity> mallChoujiangActivitieList = this.mallActivityService
				.getMallActivityType2List4ListByMallIds(mallIds);
		if (mallChoujiangActivitieList != null && mallChoujiangActivitieList.size() > 0) {
			for (MallActivity mallActivity : mallChoujiangActivitieList) {
				for (MallListVO mallListVO : mallVOList) {
					if (mallActivity.getMallId().longValue() == mallListVO.getMall().getId().longValue()) {
						IdAndTitleAndCountVO cjActivityIdAndTitleAndCountVO = new IdAndTitleAndCountVO();
						cjActivityIdAndTitleAndCountVO.setId(mallActivity.getId());
						cjActivityIdAndTitleAndCountVO.setTitle(mallActivity.getActivityTitle());
						mallListVO.setCjActivityIdAndTitleAndCountVO(cjActivityIdAndTitleAndCountVO);
					}
				}
			}
		}

		return mallVOList;
	}

	private List<Mall> getMallListFromSearchResultIdDO(List<SearchResultIdDO> searchResultIdDOList) {
		if (searchResultIdDOList == null || searchResultIdDOList.size() <= 0) {
			return null;
		}

		List<Long> mallIdList = new ArrayList<>();
		for (SearchResultIdDO searchResultIdDO : searchResultIdDOList) {
			mallIdList.add(searchResultIdDO.getId());
		}
		List<Mall> mallList = this.mallService.getMallListByMallIds(mallIdList);
		return mallList;
	}
}